﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.IO;
using DeviceSDK.LED.NuoWa.JT100;
using BaseHelper.Text;

namespace DeviceSDK.LED.NuoWa
{
    class NuoWa_JT100
    {
        //private static SortedList<string, JT100_SDK> slJT100 = new SortedList<string, JT100_SDK>();

        //获取错误代码
        public static string Get_Error(int ret)
        {
            switch (ret)
            {
                case 0:
                    return "操作成功！";

                case 1:
                    return "操作失败！";

                case 2:
                    return "发送节目单失败！";

                case 3:
                    return "保存节目单到磁盘失败！";

                case 4:
                    return "有素材文件格式不支持！";

                case 5:
                    return "素材内容为空！";

                case -1:
                    return "操作失败！";

                default:
                    return "未知错误！";
            }
        }



        public static JT100_SDK Connect(string IP,int Port)
        {
            JT100_SDK jt100 = new JT100_SDK();
            if (jt100.Connect(IP,Port))
            {
                return jt100;
            }
            return null;
        }

        public static int Send_File(string IP, int Port,string FilePath,string RemotePath)
        {
            if (!File.Exists(FilePath))
                return 1;

            JT100_SDK jt100 = Connect(IP,Port);
            if (jt100 == null)
                return 1;

            byte[] fileName=Encoding.UTF8.GetBytes(RemotePath);
            byte[] fileBody=File.ReadAllBytes(FilePath);
            bool ret = jt100.SendFile(fileName, fileBody);
            jt100.Close();

            return ret? 0:1;
        }


        public static int Get_PlaybillContent(string IP, int Port, ref string Content)
        {
            JT100_SDK jt100 = Connect(IP, Port);
            if (jt100 == null)
                return 1;

            Content = jt100.GetPlaybillContent();
            jt100.Close();
            
            return Content==null?1:0;
        }

        public static int Set_PlaybillID(string IP, int Port, string ID)
        {
            JT100_SDK jt100 = Connect(IP, Port);
            if (jt100 == null)
                return 1;
            bool ret = false;
            int playbillID =0;
            if(int.TryParse(ID,out playbillID))
            {
                ret = jt100.SetPlaybill(playbillID);
            }
            jt100.Close();
            return ret ? 0:1;
        }

        public static int Set_VirtualCheck(string IP, int Port, bool IsOpen, string DefaultPlayBillID = "0", int TimeOut = 60)
        {
            JT100_SDK jt100 = Connect(IP, Port);
            if (jt100 == null)
                return 1;
            bool ret = false;
            int playbillID = Convert.ToInt32(DefaultPlayBillID);
            if (int.TryParse(DefaultPlayBillID, out playbillID))
            {
                if(IsOpen)
                    jt100.SetVirtualCheck(VirtualCheckState.Close);

                ret = jt100.SetVirtualCheck(IsOpen ? VirtualCheckState.Open : VirtualCheckState.Close, TimeOut, playbillID);
            }
            jt100.Close();
            return ret ? 0 : 1;
        }

        public static int Get_ScreenCap(string IP, int Port, ref Image Img)
        {
            JT100_SDK jt100 = Connect(IP, Port);
            if (jt100 == null)
                return 1;

            int ret = -1;
            Img = jt100.GetScreenCap();
            ret = Img == null ? 1 : 0;
            jt100.Close();
            return ret;
        }

        public static int Get_LedCheckErrorCount(string IP, int Port)
        {
            JT100_SDK jt100 = Connect(IP, Port);
            if (jt100 == null)
                return -1;

            int ret = jt100.Get_LedCheckErrorCount();

            jt100.Close();

            return ret;
        }

        public static int Get_LedCheckResult(string IP, int Port, ref string Result)
        {
            JT100_SDK jt100 = Connect(IP, Port);
            if (jt100 == null)
                return -1;

            Result = jt100.Get_LedCheckResult();

            jt100.Close();

            return 0;
        }


        //重启
        public static int Set_Reboot(string IP, int Port)
        {
            JT100_SDK jt100 = Connect(IP, Port);
            if (jt100 == null)
                return 1;

            int ret = jt100.Reboot()?0:1;

            jt100.Close();

            return ret;
        }

        //获得-屏幕状态
        public static int Get_Screen_Status(string IP, int Port, ref int Value)
        {
            JT100_SDK jt100 = Connect(IP, Port);
            if (jt100 == null)
                return 1;


            int ret=-1;
            ScreenState ss = jt100.GetScreenState();
            if (ss == ScreenState.Open)
                ret=0;
            else if(ss==ScreenState.Close)
                ret=1;
            else
                ret=-1;
            jt100.Close();
            return ret;
        }

        //设置-屏幕-关闭
        public static int Set_Screen_Close(string IP, int Port)
        {
            JT100_SDK jt100 = Connect(IP, Port);
            if (jt100 == null)
                return 1;

            int ret = jt100.SetScreenState(ScreenState.Close) ? 0 : 1;
            jt100.Close();
            return ret;

        }

        //设置-屏幕-开启
        public static int Set_Screen_Open(string IP, int Port)
        {
            JT100_SDK jt100 = Connect(IP, Port);
            if (jt100 == null)
                return 1;

            int ret = jt100.SetScreenState(ScreenState.Open) ? 0 : 1;
            jt100.Close();
            return ret;
        }

        //获得-屏幕亮度
        public static int Get_Brightness(string IP, int Port, ref int Value)
        {
            JT100_SDK jt100 = Connect(IP, Port);
            if (jt100 == null)
                return 1;

            Value= jt100.GetScreenLight();
            int ret = Value == -1 ? 1 : 0;
            jt100.Close();
            return ret;
        }

        //设置-屏幕亮度
        public static int Set_Brightness(string IP, int Port, int Value)
        {
            JT100_SDK jt100 = Connect(IP, Port);
            if (jt100 == null)
                return 1;

            int ret = jt100.SetScreenLight(Value) ? 0 : 1;
            jt100.Close();
            return ret;
        }

        //设置-时间
        public static int Set_Time(string IP, int Port)
        {
            JT100_SDK jt100 = Connect(IP, Port);
            if (jt100 == null)
                return 1;

            int ret = jt100.SetCardTime() ? 0 : 1;
            jt100.Close();
            return ret;
        }

        //发送-节目
        public static LEDSendResult Send_Programs(bool IsTempPlaybill, string IP, int Port, int Width, int Height, PlaybillProgramInfo[] Programs, string PlaybillID, bool isCheckResult=false)
        {
            JT100_SDK jt100 = Connect(IP,Port);
            LEDSendResult result = new LEDSendResult(isCheckResult);
            if (jt100 == null)
            {
                result.RetCode = 1;
                return result ;
            }
            PlayBill pb=new PlayBill();

            //SortedList<int, List<PlaybillProgramInfo>> slScreenProgram = new SortedList<int, List<PlaybillProgramInfo>>();
            //foreach (PlaybillProgramInfo p in Programs)
            //{
            //    if (!slScreenProgram.ContainsKey(p.ScreenAreaID))
            //    {
            //        slScreenProgram.Add(p.ScreenAreaID,new List<PlaybillProgramInfo>());
            //    }
            //    slScreenProgram[p.ScreenAreaID].Add(p);
            //}

            //foreach(int ScreenID in slScreenProgram.Keys)
            //{
            //    List<PlaybillProgramInfo> plist = slScreenProgram[ScreenID];
            //    int playTime = 0;
            //    foreach (PlaybillProgramInfo pinfo in plist)
            //    {
            //        playTime += pinfo.PlayTime;
            //    }
            //    pb.AddPage(playTime*10,1,1);   // pinfo.Affect == 0 ? 1 : 255, pinfo.Affect  //pinfo.PlayTime * 10


            bool IsOnlyPage = false;
            foreach(PlaybillProgramInfo pinfo in Programs)
            {
                //PlaybillProgramInfo pinfo =dic.Value  Programs[i];
                 
                //pinfo.ScreenAreaID == 0;
                if (pinfo.ScreenAreaID == 0)
                {
                    IsOnlyPage=false;
                    pb.AddPage(pinfo.PlayTime * 10, pinfo.IsEffectRand == 0 ? 1 : 255, pinfo.IsEffectRand);  //pinfo.PlayTime * 10
                }
                else
                {
                    if (!IsOnlyPage)
                    {
                        int PlayTime = 0;
                        foreach (PlaybillProgramInfo p in Programs)
                        {
                            if (p.ScreenAreaID != 0)
                                PlayTime += p.PlayTime;
                        }
                        pb.AddPage(PlayTime * 10, 1,1);  //pinfo.PlayTime * 10
                        IsOnlyPage = true;                        
                    }
                }

                switch (pinfo.Type)
                {
                    case "picture":
                        if (!File.Exists(pinfo.FilePath))
                            continue;
                        pb.AddImage(pinfo.FilePath, pinfo.PlayTime * 10, pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height);
                        break;

                    case "video":
                        if (!File.Exists(pinfo.FilePath))
                        {
                            pb.RemoveLastPage();
                            continue;
                        }
                        pb.AddVideo(pinfo.FilePath, pinfo.PlayTime * 10, pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height);
                        break;

                    case "rtf":
                        pb.RemoveLastPage();
                        break;

                    case "text":
                        pinfo.Text = pinfo.Text.Replace("\n", "\r\n").Replace("\r\r","\r");
                        if (pinfo.Text == "")
                        {
                            pb.RemoveLastPage();
                            continue;
                        }
                        pb.AddString(pinfo.Text, pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height
                            , pinfo.FontName, pinfo.FontSize, pinfo.FontIsBold ? 1 : 0, pinfo.PositionHor, pinfo.PositionVer, pinfo.LineSpace, 0, pinfo.FontColor,  pinfo.IsEffectRand, pinfo.IsEffectRand, 1, pinfo.PlayTime * 10);
                        break;
                }
            }
                
            //}
            if (pb.Count == 0)
            {
                result.RetCode = 5;
                return result;
            }

            int playbillId = PlaybillID == "" ? IsTempPlaybill ? 2 : 1 : Convert.ToInt32(PlaybillID);
            int ret = 1;
       
            if (jt100.SendPlaybill(playbillId, pb))
            {
              
                if (jt100.SetPlaybill(playbillId))
                {

                    if (isCheckResult)
                    {
                        result.SendContent = pb.Create();
                        result.ReplayContent = jt100.GetPlaybillContent();
                    }

                    jt100.CleanFile(CleanFileType.NoUse);

                    ret = 0;
                }
                else
                    ret = 1;
            }
            else
                ret = 2;


            if (!IsTempPlaybill)
            {
                //jt100.SetVirtualCheck(VirtualCheckState.Close);
                //jt100.SetVirtualCheck(VirtualCheckState.Open, Config.Release_Play_TimeOut * 60, 1);
            }

            jt100.Close();
            result.RetCode = ret;

            return result;

        }

        internal static int Get_LedCheckCount(string IP)
        {
            throw new NotImplementedException();
        }
    }
}
